package com.bocai.service.impl;

import com.bocai.bean.CompetitionHot;
import com.bocai.bean.CompetitionInfo;
import com.bocai.bean.SysTabManager;
import com.bocai.mapper.CompetitionHotMapper;
import com.bocai.mapper.CompetitionMapper;
import com.bocai.mapper.SysTabManagerMapper;
import com.bocai.service.ICompetitionService;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.ObjUtils;
import com.bocai.utils.ResponseData;
import com.bocai.vo.CompetitionInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CompetitionServiceImpl implements ICompetitionService {

    @Autowired
    CompetitionHotMapper competitionHotMapper;

    @Autowired
    CompetitionMapper competitionMapper;

    @Autowired
    SysTabManagerMapper sysTabManagerMapper;


    @Override
    public List<CompetitionHot> selectCompetitionLists(CompetitionHot competitionHot) {
        return competitionHotMapper.selectCompetitions(competitionHot);
    }

    @Override
    public List<Map<String, Object>> selectCountrys(CompetitionHot competitionHot) {
        List<Map<String, Object>> lists = competitionHotMapper.selectCountryLists(competitionHot);
        return lists;
    }

    @Override
    public int insertCompetition(CompetitionHot competitionHot) {
        return competitionHotMapper.insertSelective(competitionHot);
    }

    @Override
    public int updateCompetition(CompetitionHot competitionHot) {
        return competitionHotMapper.updateByPrimaryKeySelective(competitionHot);
    }

    @Override
    public CompetitionHot selectHotCompetitionId(Integer competitionId) {
        return competitionHotMapper.selectByPrimaryKey(competitionId);
    }

    @Override
    public ResponseData selectAllCompetition(Integer countryId,Integer status,Integer tabId,String keyword, ResponseData data) {
        data.setCodeEnum(CodeEnum.SUCCESS);

        Map<String,Object> map = new HashMap<>();
        map.put("status",status);
        map.put("keyword",keyword);
        map.put("tabId",tabId);
        map.put("countryId",countryId);
        List<CompetitionInfoVo> list = this.competitionMapper.selectAllCompetition(map);

        List<CompetitionInfoVo> temp = new ArrayList<>();

        for (CompetitionInfoVo info : list) {

            //查询标签对象
            String competitionTab = info.getCompetitionTab();
            if (ObjUtils.isNotEmpty(competitionTab)) {
                List<SysTabManager> tabs = this.sysTabManagerMapper.selectTabLists(competitionTab);
                info.setTabs(tabs);
            }

            temp.add(info);
        }

        data.setResults(temp);
        return data;
    }


    @Override
    public int insertCompetitions(List<CompetitionInfo> competitionInfos) {
        for (CompetitionInfo info : competitionInfos) {
            this.competitionMapper.insertSelective(info);
        }
        return 0;
    }

    @Override
    public int updateCompetitions(List<CompetitionInfo> competitionInfos) {
        for (CompetitionInfo info : competitionInfos) {
            this.competitionMapper.updateByPrimaryKeySelective(info);
        }
        return 0;
    }
}
